/**
 * 
 */
package com.ideazd.api.controller;


import java.util.Date;
import java.util.Map;


import com.ideazd.api.model.FeedBack;
import com.ideazd.api.model.LoginLog;
import com.ideazd.api.model.SmsCode;
import com.ideazd.api.model.User;
import com.ideazd.api.model.dto.RetDTO;
import com.ideazd.api.msm.SentMessage;
import com.ideazd.api.service.UserService;
import com.ideazd.api.tools.DateUtil;
import com.ideazd.api.tools.SysUtil;
import com.ideazd.api.util.RandomUtil;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Logger;

/**
 * @author yzz
 * @create 2016年3月24日 上午10:47:22
 */
@ControllerBind(controllerKey="user")
public class UserController extends Controller {
	private static final Logger log = Logger.getLogger(UserController.class);
	private UserService userService = new UserService();
	
	/**
	 * 获取验证码
	 */
	public void getSmsCode(){
		String phone = getPara("phone");
		RetDTO ret = new RetDTO();
		
		if(StrKit.isBlank(phone)){
			ret.setCode("-2");
			ret.setMsg("必传参数不允许为空");
			renderJson(ret);
			return;
		}
		try {
			String verifyCode =RandomUtil.randomFor6();//自定义验证码
			SentMessage.sentVerifyCode(phone,verifyCode);//发送短信
			
		   SmsCode smsCode = new SmsCode()
		   .set("smsid", SysUtil.getUUID())
		   .set("phone", phone)
		   .set("smstype", "0")
		   .set("checkcode", verifyCode)
		   .set("sendtime", new Date());
			
			if(smsCode.save()){
				ret.setCode("0");
				ret.setMsg("成功");
			}
			
		} catch (Exception e) {
			ret.setCode("-1");
			ret.setMsg(e.getMessage());
		}
		renderJson(ret);
		return;
	}
	
	/**
	 * 用户登录
	 */
	@SuppressWarnings("null")
	public void login(){
		String account = getPara("account");
		String useType = getPara("useType");
		String smsCode = getPara("smsCode");
		String ip = getPara("ip");
		String gender = getPara("gender");
		String nickName = getPara("nickName");
		String headPortrait = getPara("headPortrait");
		RetDTO ret = new RetDTO();
		
		if(StrKit.isBlank(account) || StrKit.isBlank(useType)){
			ret.setCode("-2");
			ret.setMsg("必传参数不允许为空");
			renderJson(ret);
			return;
		}
		
		try {
			
			if("1".equals(useType)){
				
				if(StrKit.isBlank(smsCode)){
					ret.setCode("-2");
					ret.setMsg("必传参数不允许为空");
					renderJson(ret);
					return;
				}
				
				SmsCode checkCode = SmsCode.dao.findFirst("select * from sta_smscode where phone = '"+account+"' and checkcode = '"+smsCode+"'");
				
				if(null == checkCode){
					ret.setCode("-3");
					ret.setMsg("验证码输入有误");
					renderJson(ret);
					return;
				}else{
					
					if((new Date().getTime() - checkCode.getDate("sendtime").getTime()) > 300000){
						ret.setCode("-4");
						ret.setMsg("验证码已过期");
						renderJson(ret);
						return;
					}
					
				}
			}
			
			User user = userService.findUserByAccount(account);
			
			if(null == user){
				String userId = SysUtil.getUUID();
				user.set("useid", userId);
				user.set("account", account);
				user.set("nickname", nickName);
				user.set("gender", gender);
				user.set("headportrait", headPortrait);
				user.set("userstatus", "0");
				user.set("logintime", new Date());
				user.set("usetype", useType);
				user.save();
			}
			
			LoginLog log = new LoginLog();
			log.set("logid", SysUtil.getUUID());
			log.set("useid", user.get("useid"));
			log.set("logtime", new Date());
			log.set("logip", ip);
			log.save();
			
			ret.put("useId", user.get("useid"));
			ret.put("account", user.get("account"));
			ret.put("nicknName", user.get("nickname"));
			ret.put("gender", user.get("gender"));
			ret.put("headPortrait", user.get("headportrait"));
			ret.put("userStatus", user.get("userstatus"));
			ret.put("useType", user.get("usetype"));
			
			
			ret.setCode("0");
			ret.setMsg("成功");
			
			
			
		} catch (Exception e) {
			ret.setCode("-1");
			ret.setMsg(e.getMessage());
		}
		renderJson(ret);
		return;
	}
	
	/**
	 * 获取用户详情
	 */
	public void getUserDetail(){
		String useId = getPara("useId");
		RetDTO ret = new RetDTO();
		
		if(StrKit.isBlank(useId)){
			ret.setCode("-2");
			ret.setMsg("必传参数不允许为空");
			renderJson(ret);
			return;
		}
		try {
			User user = userService.findUserById(useId);
			
			ret.put("useId", user.get("useid"));
			ret.put("account", user.get("account"));
			ret.put("nicknName", user.get("nickname"));
			ret.put("gender", user.get("gender"));
			ret.put("phone", user.get("phone"));
			ret.put("personality", user.get("personality"));
			ret.put("headPortrait", user.get("headportrait"));
			ret.put("userStatus", user.get("userstatus"));
			ret.put("useType", user.get("usetype"));
			
			ret.setCode("0");
			ret.setMsg("成功");
			
		} catch (Exception e) {
			ret.setCode("-1");
			ret.setMsg(e.getMessage());
		}
		renderJson(ret);
		return;
	}
	
	/**
	 * 修改用户信息
	 */
	public void editUserInfo(){
		String useId = getPara("useId");
		String headPortrait = getPara("headPortrait");
		String nickName = getPara("nickName");
		String personality = getPara("personality");
		String gender = getPara("gender");
		RetDTO ret = new RetDTO();
		
		if(StrKit.isBlank(useId)){
			ret.setCode("-2");
			ret.setMsg("必传参数不允许为空");
			renderJson(ret);
			return;
		}
		
		try {
			
			if(!StrKit.isBlank(headPortrait)){
				
				Map<String, String> imaPath = SysUtil.uploadImg(getRequest(),headPortrait,"/upload/userheadPortrait"+"/"+useId+"/images"+"/"+DateUtil.formatDate(new Date(),"yyyyMMdd")+"/");
				
				headPortrait = imaPath.get("diskPath");
				
			}
			
			User user = new User();
			user.set("useid", useId);
			user.set("nickname", nickName);
			user.set("gender", gender);
			user.set("personality", personality);
			
			if(!StrKit.isBlank(headPortrait)){
				user.set("headportrait", headPortrait);
			}
			
			user.update();
			
			ret.put("useId", user.get("useid"));
			ret.put("nickName", user.get("nickname"));
			ret.put("personality", user.get("personality"));
			ret.put("headPortrait", user.get("headportrait"));
			
			
			
			
			ret.setCode("0");
			ret.setMsg("成功");
			
		} catch (Exception e) {
			ret.setCode("-1");
			ret.setMsg(e.getMessage());
		}
		renderJson(ret);
		return;
	}
	
	
	/**
	 * 建议反馈
	 */
	public void sendFeedBack(){
		String useId = getPara("useId");
		String feeInfo = getPara("feeInfo");
		String contactMode = getPara("contactMode");
		RetDTO ret = new RetDTO();
		
		if(StrKit.isBlank(useId) || StrKit.isBlank(feeInfo) || StrKit.isBlank(contactMode)){
			ret.setCode("-2");
			ret.setMsg("必传参数不允许为空");
			renderJson(ret);
			return;
		}
		try {
			FeedBack feedBack = new FeedBack();
			feedBack.set("feeid", SysUtil.getUUID());
			feedBack.set("useid", useId);
			feedBack.set("feeinfo", feeInfo);
			feedBack.set("contactmode", contactMode);
			feedBack.set("createtime", new Date());
			feedBack.set("handlestate", "0");
			feedBack.save();
			
			ret.setCode("0");
			ret.setMsg("成功");
			
		} catch (Exception e) {
			ret.setCode("-1");
			ret.setMsg(e.getMessage());
		}
		renderJson(ret);
		return;
	}
	
	/**
	 * 提醒通知
	 */
	public void setOpenSms(){
		String useId = getPara("useId");
		String opneSms = getPara("opneSms");
		RetDTO ret = new RetDTO();
		
		if(StrKit.isBlank(useId) || StrKit.isBlank(opneSms)){
			ret.setCode("-2");
			ret.setMsg("必传参数不允许为空");
			renderJson(ret);
			return;
		}
		try {
			User user = new User();
			user.set("useid", useId);
			user.set("opensms", opneSms);
			
			ret.setCode("0");
			ret.setMsg("成功");
			
		} catch (Exception e) {
			ret.setCode("-1");
			ret.setMsg(e.getMessage());
		}
		renderJson(ret);
		return;
	}
	

}
